package com.sws.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.sws.Constant;
import com.sws.model.UserModel;
import com.sws.model.bean.B2SUserBean;
import com.sws.model.bean.C2SUserBean;
import com.sws.model.bean.S2CUserBean;
import com.sws.service.UserService;
import com.sws.utils.CUtils;
import com.sws.utils.PinYinUtils;
import com.sws.utils.ThreadPool;
import com.sws.utils.UserParse;

@Controller
public class UserController {
	
	private Logger logger = LoggerFactory.getLogger(UserController.class);
	private static final String LOCATION_PATH = "C:/ServerUpload/SWSUpload/student";
	private static final String REQUEST_PATH = "SWSUpload/student/";
//	private static final String LOCATION_PATH = "E:/serverfile/SWSUpload/student";
	@Autowired
	private UserService service;

	@ResponseBody
	@RequestMapping("/s/login")
	public String login(C2SUserBean user, HttpServletRequest request) {
		logger.info("======> 登录");
		HttpSession session = request.getSession();
		JSONObject result = new JSONObject();
		try {
			S2CUserBean  userBean = service.login(user);
			if (user != null) {
				session.setAttribute("userId", userBean.getId());
				result.put("status", Constant.INSTANSE.OK);
				result.put("data", userBean);
				result.put("msg", "登录成功！");
			} else {
				result.put("status", Constant.INSTANSE.ACCOUNT_PASSWORD_INVALID);
				result.put("msg", "帐号或密码错误！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("===> {}", "服务器错误");
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/uploadStudent")
	public String batchUploadStudent(MultipartFile file, HttpServletRequest request) {
		logger.info("======> 批量添加学生用户");
		JSONObject result = new JSONObject();
		String fileName = CUtils.getDateTime() + "_" + file.getOriginalFilename();
		File dir = new File(LOCATION_PATH);
		//判断上传文件的保存目录是否存在
		if (!dir.exists() && !dir.isDirectory()) {
			System.out.println("创建目录");
			dir.mkdirs();
		}
		try {
			File saveFile = new File(LOCATION_PATH, fileName);
			file.transferTo(saveFile);
			service.addStudent(UserParse.xlsParse(LOCATION_PATH+"/"+fileName, 0));
			
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "批量添加学生用户成功！");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/addStudent")
	public String addStudent(B2SUserBean bean) {
		logger.info("======> 添加学生用户");
		JSONObject result = new JSONObject();
		try {
			UserModel model = new UserModel(bean);
			service.addStudent(model);
			if (model.getIsSuccess() != null && model.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "添加学生用户成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "添加学生用户失败！");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/deleteStudent")
	public String deleteStudent(B2SUserBean bean) {
		logger.info("======> 删除学生用户");
		JSONObject result = new JSONObject();
		try {
			service.deleteStudent(bean.getId());
			result.put("status", Constant.INSTANSE.OK);
			result.put("msg", "删除学生用户成功！");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
	
	@ResponseBody
	@RequestMapping("/t/modifyStudent")
	public String modifyStudent(B2SUserBean bean) {
		logger.info("======> 修改学生信息");
		JSONObject result = new JSONObject();
		try {
			UserModel model = new UserModel(bean);
			service.modifyStudent(model);
			if (model.getIsSuccess() != null && model.getIsSuccess()) {
				result.put("status", Constant.INSTANSE.OK);
				result.put("msg", "修改学生信息成功！");
			} else {
				result.put("status", Constant.INSTANSE.FAIL);
				result.put("msg", "修改学生信息失败！");
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result.put("status", Constant.INSTANSE.SERVSER_ERROR);
			result.put("msg", "服务器内部错误！");
		}
		result.put("time", new Date());
		return result.toJSONString();
	}
}
